//
//  TXFStatuesFirstView.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/6/27.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit
import SnapKit
let StautsMargin: CGFloat = 10
class TXFStatuesFirstView: UIView {

    //定义全局约束:
    
    var pictureViewConstraint : Constraint?
    
    
    var status: TXFStatues? {
        didSet{
            // 用户昵称
            nameLabel.text = status?.user?.name
            // 用户头像
            HomeImageView.sd_setImageWithURL(NSURL(string: status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
            // 时间和来源后期处理
            // 微博内容
            contentnext.text = status?.text
            sourceLab.attributedText = status?.sourceAttr
            
            //微博内容 转换
            
//            contentnext.attributedText = status?.sourceAttr
            
            
            //时间:
            timeLab.text  = getTimesStr(status?.created_at ?? "")
            
            // 认证图片
            vipImageView.image = status?.user?.verifiedImageView
            // 会员等级
            
            contentnext.attributedText = status?.textAttr
//            getWeiBoAttr(status?.text ?? "")
            expiredImageView.image = status?.user?.mbrankImageView
            
            //来源:
            
            sourceLab.attributedText  = status?.sourceAttr
            
            //卸载约束
            pictureViewConstraint?.uninstall()
            if status?.pic_urls?.count > 0 {
               pictureView.pictureInfo = status?.pic_urls
            //显示:
                pictureView.hidden = false
            //更新约束
                self.snp_makeConstraints { (make) -> Void in
                    
                    pictureViewConstraint  =  make.bottom.equalTo(pictureView).offset(10).constraint
                    
                    
                }
            
            }else{
                
                //标示没有图片
            //隐藏:
                pictureView.hidden = true
            //更新约束
            
                self.snp_makeConstraints { (make) -> Void in
                    
                    pictureViewConstraint  =  make.bottom.equalTo(contentnext).offset(10).constraint
                    
                    
                }
            
            
            }
            
            
            
        }
    }

    //正则
    
    func getWeiBoAttr(text: String){
    
    let resultAttr = NSMutableAttributedString(string: text)
        
        (text as NSString).enumerateStringsMatchedByRegex("\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]") { (count, string, range, _) -> Void in
            print(string.memory)
    
            
            let emoticon = TXFEmoticonTools.sharedTools.getSearchEmoticon((string.memory as! String))
            // 文本附件
            let att = NSTextAttachment()
            let path = emoticon?.path ?? ""
            let png = emoticon?.png ?? ""
            let name = path + png
            // 设置image
            att.image = UIImage(named: name, inBundle: TXFEmoticonTools.sharedTools.emoticonBundle, compatibleWithTraitCollection: nil)
            // 行高
            let lineHeight = UIFont.systemFontOfSize(15).lineHeight
            // 设置bounds
            att.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
            
            let attr = NSAttributedString(attachment: att)
            resultAttr.replaceCharactersInRange(range.memory, withAttributedString: attr)

            
        }
    
    
    }
    
    
    func getTimesStr(createAt: String) ->String{
    //设置时间格式:
        let  df = NSDateFormatter()
        
        df.locale = NSLocale(localeIdentifier: "en_US")
        
        df.dateFormat = "EEE MM dd HH:mm:ss z yyyy"
        
        let wbDate =  df.dateFromString(createAt)!
        
        let  isThisYear = wbDateIsThisYear(wbDate)
        
        if isThisYear {
            let calender = NSCalendar.currentCalendar()
            if calender.isDateInToday(wbDate){
                let s = NSDate().timeIntervalSinceDate(wbDate)
                if s < 60 {
                    return "刚刚"
                }else if s >= 60 && s < 60 * 60 {
                    return "\(Int(s / 60))分钟前"
                }else {
                    return "\(Int(s / 3600))小时前"
                }
                
                
            } else if calender.isDateInYesterday(wbDate){
                df.dateFormat = "昨天 HH:mm"
                return df.stringFromDate(wbDate)
            }else{
                df.dateFormat = "MM:dd HH:mm"
                return df.stringFromDate(wbDate)
            }
            
            
            
        }else {
            df.dateFormat = "yyyy年MM月dd日"
            return df.stringFromDate(wbDate)
        }
    }
    
    func wbDateIsThisYear(wbDate: NSDate) ->Bool{
    
    let df = NSDateFormatter()
        
    df.locale = NSLocale(localeIdentifier: "en_US")
    df.dateFormat = "yyyy"
        
    let wbStr = df.stringFromDate(wbDate)
    
    let currentStr = df.stringFromDate(NSDate())
        
        
        if wbStr == currentStr {
        
        return true
        
        }else {
        
        
        return false
        
        }
    
   
    }
    
    
    func getTimeStr(createAt: String) -> String{
    let df = NSDateFormatter()
        
        df.dateFormat = "EEE MM dd HH:mm:ss z yyyy"
        
        df.locale = NSLocale(localeIdentifier: "en_US")
        
        let wbDate = df.dateFromString(createAt)!
        
        let isThisYear = wbDateIsThisYear(wbDate)
        
        if isThisYear {
            
            let calender = NSCalendar.currentCalendar()
            
            if calender.isDateInToday(wbDate) {
                let s = NSDate().timeIntervalSinceDate(wbDate)
                
                if s < 60 {
                    return "刚刚"
                }else if s >= 60 && s < 60 * 60 {
                    return "\(Int(s / 60))分钟前"
                }else{
                    return "\(Int(s / 3600))小时前"
                }
                
            } else if calender.isDateInYesterday(wbDate){
                
                df.dateFormat = "昨天 HH:mm"
                
                return df.stringFromDate(wbDate)
            }else{
                df.dateFormat = "MM月dd日 HH:mm"
                return df.stringFromDate(wbDate)
            }
            
        }else {
            
            df.dateFormat = "yyyy年MM月dd日 HH:mm"
            return df.stringFromDate(wbDate)
        }
    }
    
    func wbIsThisYear(wbDate: NSDate) -> Bool{
        let df = NSDateFormatter()
        df.locale = NSLocale(localeIdentifier: "en_US")
        df.dateFormat = "yyyy"
        let wbStr = df.stringFromDate(wbDate)
        let currentStr = df.stringFromDate(NSDate())
        
        if wbStr == currentStr {
            return true
        }else {
            return false
        }
        
    }
    
    
   override init(frame: CGRect) {
        super.init(frame: frame)
    
    setupUI()
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //创建头像:


    
//懒加载数据:
    //头像:
    private lazy var HomeImageView : UIImageView  = {
    
    let img  = UIImageView(image: UIImage(named: "avatar_default_big"))
   
    return img
    }()
    //文字信息:
    private lazy var nameLabel : UILabel = {
    
    let lab = UILabel()
    lab.font = UIFont.systemFontOfSize(10)
    lab.textColor = UIColor.orangeColor()

    return lab
    }()
//    common_icon_membership_level1
    //等级图片
    private lazy var expiredImageView : UIImageView  = {
        
        let img  = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
        
        
        return img
    }()
    
    private lazy var timeLab : UILabel = {
    
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(10)
        lab.textColor = UIColor.orangeColor()
        lab.text = "当前时间"
        return lab
    
    
    }()
    private lazy var sourceLab : UILabel = {
        
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(10)
        lab.textColor = UIColor.orangeColor()
        lab.text = "飞飞weibo.com"
        return lab
        
        
    }()
    
    private lazy var vipImageView : UIImageView  = {
        
        let img  = UIImageView(image: UIImage(named: "avatar_vgirl"))
        
        
        return img
    }()
    
    //创建文字信息:
    
    private lazy var contentnext: UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15, maxWith: SCREENW - 20 )

    //如果原创微博中有图片(懒加载)
    private lazy var pictureView : TXFPictureView = TXFPictureView()
    
    
  private  func setupUI(){
    
    addSubview(HomeImageView)
    addSubview(nameLabel)
    addSubview(expiredImageView)
    addSubview(timeLab)
    addSubview(sourceLab)
    addSubview(vipImageView)
    addSubview(contentnext)
    addSubview(pictureView)
    HomeImageView.snp_makeConstraints { (make) -> Void in
       make.top.left.equalTo(self).offset(10)
        
        make.size.equalTo(CGSize(width: 35, height: 35))
        
    }
    nameLabel.snp_makeConstraints { (make) -> Void in
        
        make.top.equalTo(HomeImageView)
        
        make.left.equalTo(HomeImageView.snp_right).offset(10)
        
    }
    
    expiredImageView.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(HomeImageView)
        make.left.equalTo(nameLabel.snp_right).offset(10)
        
    }
    timeLab.snp_makeConstraints { (make) -> Void in
        
        make.left.equalTo(nameLabel)
        
        make.bottom.equalTo(HomeImageView.snp_bottom)
        
        
    }
    
    sourceLab.snp_makeConstraints { (make) -> Void in
        
        make.left.equalTo(timeLab.snp_right).offset(10)
        
        make.bottom.equalTo(timeLab.snp_bottom)
    }
    
    vipImageView.snp_makeConstraints { (make) -> Void in
        
        make.centerX.equalTo(HomeImageView.snp_right)
        make.centerY.equalTo(HomeImageView.snp_bottom)
        
    }
    
    contentnext.snp_makeConstraints { (make) -> Void in
        
        make.left.equalTo(HomeImageView)
        
        make.top.equalTo(HomeImageView.snp_bottom).offset(10)
        
        
        
    }
    
    pictureView.snp_makeConstraints { (make) -> Void in
        
        make.top.equalTo(contentnext.snp_bottom).offset(10)
        make.leading.equalTo(contentnext)
        
//        make.size.equalTo(CGSize(width: 100, height: 100))
        
    }
    
    
    //本身的约束
    
    self.snp_makeConstraints { (make) -> Void in
        
    pictureViewConstraint  =  make.bottom.equalTo(pictureView).offset(10).constraint

        
    }
    
    
    
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
